﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
namespace TDLib.Security
{
    /// <summary>
    /// 解密类
    /// </summary> 
    public class DecryptTransformer
    {
        private EncryptionAlgorithm algorithmID;
        private byte[] initVec;
        private byte[] encKey;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deCryptId"></param>
        public DecryptTransformer(EncryptionAlgorithm deCryptId)
        {
            algorithmID = deCryptId;
        }

        /// <summary>
        /// 获取或设置加密的偏移向量 
        /// </summary>
        public byte[] IV
        {
            get { return initVec; }
            set { initVec = value; }
        }

        /// <summary>
        /// 获取或设置加密的密钥
        /// </summary>
        public byte[] Key
        {
            get { return encKey; }
            set { encKey = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bytekey"></param>
        /// <returns></returns>
        public ICryptoTransform GetCryptoServiceProvider(byte[] bytekey)
        {
            //当数据密钥Key或者初始化向量IV为空的时候，将使用加密对象自动产生的密钥Key或者初始化向量IV 
            switch (algorithmID)
            {
                case EncryptionAlgorithm.Des:
                    {
                        System.Security.Cryptography.DES des = new DESCryptoServiceProvider();
                        des.Mode = CipherMode.CBC;
                        des.Key = bytekey;
                        des.IV = initVec;
                        return des.CreateDecryptor();
                    }
                case EncryptionAlgorithm.TripleDes:
                    {
                        TripleDES des3 = new TripleDESCryptoServiceProvider();
                        des3.Mode = CipherMode.CBC;
                        return des3.CreateDecryptor(bytekey, initVec);
                    }
                case EncryptionAlgorithm.Rc2:
                    {
                        RC2 rc2 = new RC2CryptoServiceProvider();
                        rc2.Mode = CipherMode.CBC;
                        return rc2.CreateDecryptor(bytekey, initVec);
                    }
                case EncryptionAlgorithm.Rijndael:
                    {
                        Rijndael rijndael = new RijndaelManaged();
                        rijndael.Mode = CipherMode.CBC;
                        return rijndael.CreateDecryptor(bytekey, initVec);
                    }
                default:
                    {
                        throw new CryptographicException("Algorithm ID ''" +
                        algorithmID +
                        "'' not supported.");
                    }
            }
        }

    }
}
